#!/usr/local/bin/python3

__author__ = '曹植'

from .models import *
import urllib
import random
from datetime import datetime, timedelta, date
import re
import json
from django.core.cache import cache
from django.db.models import Q
from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
# HOST = 'https://www.88pxh.com'

config = RelatedParameterConfig.objects.get()
HOST = config.host
# HOST = 'http://127.0.0.1:8000'
# 首页展示的app数目
index_app_count = config.index_app_count
# 详情页相关app数目
related_app_count = config.detail_app_count
index_start_loc = [0] * index_app_count
app_category_count = 6
recommend_article_count = 6
hot_article_count = 10
related_article_count = 6
hot_app_count = 9


def get_app_article_list():
    """
    获取全部的app文章列表
    :return:
    """
    return list(AppArticle.objects.values('id', 'title'))


def get_app_list():
    """
    获取全部的app列表
    :return:
    """
    return list(App.objects.values('id', 'title', 'icon_link'))


def get_category_app_count(category_id):
    """
    获取分类app的总数
    :param category_id:
    :return:
    """
    return AppCategoryMapping.objects.values("id", ).filter(category_id=category_id).count()


def get_category_app_list(category_id):
    app_id_list = AppCategoryMapping.objects.values('app_id').filter(category_id=category_id)
    category_app_list = list()
    for item in app_id_list:
        category_app_list.append(App.objects.values("id", "title", "icon_link").get(id=item['app_id']))
    return category_app_list


app_category_count_list = list(map(lambda x: get_category_app_count(x), range(1, app_category_count + 1)))
app_article_list = get_app_article_list()
app_list = get_app_list()


def read_from_cache(category_id):
    flag = 'index'
    global index_start_loc
    value = cache.get(flag + str(category_id))
    if not value:
        write_to_cache(category_id, index_start_loc[category_id - 1])
        index_start_loc[category_id - 1] += index_app_count
        value = cache.get(flag + str(category_id))
    data = json.loads(value)
    return data


# write cache user id
def write_to_cache(category_id, index, flag='index'):
    count = index_app_count
    if app_category_count_list[category_id - 1] - index > count:
        records = AppCategoryMapping.objects.values("app_id", ).filter(category_id=category_id)[
                  index:index + count]
    else:
        records = list(AppCategoryMapping.objects.values("app_id", ).filter(category_id=category_id)[
                       index:])
        records.extend(list(AppCategoryMapping.objects.values("app_id", ).filter(category_id=category_id)[
                            :count - len(records) + 1]))
    data_list = list()
    for record in records:
        data = dict()
        item = App.objects.values("id", "title", "icon_link").get(id=record['app_id'])
        data['id'] = item['id']
        data['title'] = item['title']
        data['icon_link'] = item['icon_link']
        data_list.append(data)
    cache.set(flag + str(category_id), json.dumps(data_list), settings.NEVER_REDIS_TIMEOUT)


def write_related_app_to_cache(app_id, category_id, flag='detail'):
    data_list = random.sample(get_category_app_list(category_id), related_app_count)
    cache.set(flag + str(app_id), json.dumps(data_list), settings.NEVER_REDIS_TIMEOUT)


def read_related_app_from_cache(app_id, category_id):
    flag = 'detail'
    global index_start_loc
    value = cache.get(flag + str(app_id))
    if not value:
        write_related_app_to_cache(app_id, category_id)
        value = cache.get(flag + str(app_id))
    data = json.loads(value)
    return data


def get_related_app_list(count, category_id):
    """
    获取同类别的相关app
    :param count:
    :param category_id:
    :return:
    """
    app_list = []
    app_records = AppCategoryMapping.objects.values('app_id', ).filter(category_id=category_id)
    if app_records:
        if len(app_records) > count:
            app_records = random.sample(list(app_records), count)
        for app_record in app_records:
            data = dict()
            item = App.objects.values("id", "title", "icon_link").get(id=app_record['app_id'])
            data['id'] = item['id']
            data['title'] = item['title']
            data['icon_link'] = item['icon_link']
            app_list.append(data)
        # print(app_list)
        return app_list


def get_yesterday_article_list():
    """
    获取昨天的全部文章列表
    :return:
    """
    today = datetime.now()
    today_time = datetime(today.year, today.month, today.day, 0, 0, 0)
    start_time = today_time + timedelta(days=-1)
    end_time = today_time
    items = ArticleNew.objects.values('id', 'title').filter(
        Q(add_time__lt=end_time) & Q(add_time__gte=start_time))
    return list(items)


def write_recommend_article_to_cache(app_id, flag='recommend_article'):
    data_list = random.sample(app_article_list, recommend_article_count)
    cache.set(flag + str(app_id), json.dumps(data_list), settings.NEVER_REDIS_TIMEOUT)


def read_recommend_article_from_cache(app_id):
    flag = 'recommend_article'
    value = cache.get(flag + str(app_id))
    if not value:
        write_recommend_article_to_cache(app_id)
        value = cache.get(flag + str(app_id))
    data = json.loads(value)
    return data


def write_hot_article_to_cache(art_id, flag='hot_article'):
    data_list = random.sample(app_article_list, hot_article_count)
    cache.set(flag + str(art_id), json.dumps(data_list), settings.NEVER_REDIS_TIMEOUT)


def read_hot_article_from_cache(art_id=None):
    """
    获取app文章详情页热点文章缓存
    :param art_id:
    :return:
    """
    flag = 'hot_article'
    value = cache.get(flag + str(art_id))
    if not value:
        write_hot_article_to_cache(art_id)
        value = cache.get(flag + str(art_id))
    data = json.loads(value)
    return data


def write_related_article_to_cache(art_id, flag='related_article'):
    data_list = random.sample(app_article_list, related_article_count)
    cache.set(flag + str(art_id), json.dumps(data_list), settings.NEVER_REDIS_TIMEOUT)


def read_related_article_from_cache(art_id=None):
    """
    获取app文章详情页相关文章缓存
    :param art_id:
    :return:
    """
    flag = 'related_article'
    value = cache.get(flag + str(art_id))
    if not value:
        write_related_article_to_cache(art_id)
        value = cache.get(flag + str(art_id))
    data = json.loads(value)
    return data


def write_related_news_to_cache(count, id):
    flag = 'related_news'
    news_list = []
    keyword_id = ArticleNew.objects.get(id=id).keyword_id
    news_records = ArticleNew.objects.values('id', 'title', 'add_time').filter(keyword_id=keyword_id)
    if len(news_records) > count:
        news_records = random.sample(list(news_records), count)
    for news_record in news_records:
        data = dict()
        data['id'] = int(news_record['id'])
        data['title'] = news_record['title']
        data['add_time'] = news_record['add_time']
        news_list.append(data)
    cache.set(flag + str(id), json.dumps(news_list, cls=DateEncoder), settings.NEVER_REDIS_TIMEOUT)


def read_related_news_from_cache(count, id):
    """
    读取相关新闻的缓存
    :param count:
    :param id:
    :return:
    """
    flag = 'related_news'
    value = cache.get(flag + str(id))
    if not value:
        write_related_news_to_cache(count, id)
        value = cache.get(flag + str(id))
    data = json.loads(value)
    return data


def write_hot_app_to_cache(flag='hot_app'):
    data_list = random.sample(app_list, hot_app_count)
    cache.set(flag, json.dumps(data_list), settings.NEVER_REDIS_TIMEOUT)


def read_hot_app_from_cache():
    """
    读取热门app缓存
    :return:
    """
    flag = 'hot_app'
    value = cache.get(flag)
    if not value:
        write_hot_app_to_cache()
        value = cache.get(flag)
    data = json.loads(value)
    return data


class DateEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime("%Y-%m-%d")
        else:
            return json.JSONEncoder.default(self, obj)


def write_index_show_article_list_cache(count, k_id):
    """
    首页展示文章缓存
    :param count:
    :param k_id:
    :return:
    """
    index_show_article_list = list()
    keyword_item = SpiderNewsKeyword.objects.get(id=k_id)
    if keyword_item:
        items = SpiderNewsKeywordRelation.objects.values("keyword_id", ).filter(p_id=k_id)
        if items:
            if len(items) > count:
                items = random.sample(list(items), count)
            for item in items:
                data = dict()
                data['category_id'] = item['keyword_id']
                data['category'] = SpiderNewsKeyword.objects.get(id=data['category_id']).keyword
                article_items = ArticleNew.objects.values("id", "title",
                                                          "add_time").filter(keyword_id=data['category_id'])
                if article_items:
                    article_item = article_items[0]
                else:
                    ran = random.randint(1, 10000)
                    article_item = ArticleNew.objects.values("id", "title",
                                                             "add_time")[ran]
                data['id'] = article_item['id']
                data['title'] = article_item['title']
                data['add_time'] = article_item['add_time']
                index_show_article_list.append(data)
            cache.set('index_article' + str(k_id),
                      json.dumps(index_show_article_list, cls=DateEncoder),
                      settings.NEVER_REDIS_TIMEOUT)


def read_index_show_article_list_cache(count, k_id):
    """
    首页展示文章缓存
    :param count:
    :param k_id:
    :return:
    """
    flag = 'index_article' + str(k_id)
    value = cache.get(flag)
    if not value:
        write_index_show_article_list_cache(count, k_id)
        value = cache.get(flag)
    data = json.loads(value)
    return data


def write_credit_class_article_list_cache(count, typ):
    items = list(ArticleNew.objects.values("id", "title").filter(keyword_id=typ))
    index_show_article_list = random.sample(items, count)
    cache.set('credit_class' + str(typ), json.dumps(index_show_article_list, cls=DateEncoder),
              settings.NEVER_REDIS_TIMEOUT)


def read_credit_class_article_list_cache(count, typ):
    """
    读取网贷课堂各大板块文章缓存
    :param count:
    :param typ:
    :return:
    """
    flag = 'credit_class' + str(typ)
    value = cache.get(flag)
    if not value:
        write_credit_class_article_list_cache(count, typ)
        value = cache.get(flag)
    data = json.loads(value)
    return data


def app_list_views(request):
    url = request.get_full_path()
    key = ''
    shorthand = str(url.split('/')[2])
    category_id = AppCategory.objects.get(shorthand=shorthand).id
    if 'page' in url:
        page = int(re.search('\d+', url, flags=0).group())
    else:
        page = 1
    pageSize = 10
    if 'search_data' in url:
        key = url.split('/')[url.split('/').index('search_data') + 1]
        key = urllib.parse.unquote(key)
        records = App.objects.filter(title__contains=key).order_by('-id')[
                  (page - 1) * pageSize:page * pageSize]
        list_total = App.objects.filter(title__contains=key).count()
    else:
        if category_id:
            app_items = AppCategoryMapping.objects.filter(category_id=category_id).order_by('-app_id')[
                        (page - 1) * pageSize:page * pageSize]
            records = []
            for app_item in app_items:
                record = App.objects.get(id=app_item.app_id)
                records.append(record)
            list_total = AppCategoryMapping.objects.filter(category_id=category_id).count()
    result = dict()
    result['listTotal'] = 0
    result['listPage'] = 0
    result['search_data'] = key
    if records:
        data_list = []
        for record in records:
            data = dict()
            data['id'] = record.id
            data['title'] = record.title
            data['icon_link'] = record.icon_link
            if record.introduce:
                data['introduce'] = record.introduce
            else:
                data[
                    'introduce'] = '是一个贷款服务平台，致力于为上班族、企业主、自由职业者、小微企业主提供贷款申请、搜索、推荐、咨询服务。' \
                                   '通过来借APP即可进行小额贷款、大额贷款、信用贷款、信用卡等的申请，还可通过“来借之家”在线社区交流借款心得、分享“新口子”。'
            data_list.append(data)
        result['data'] = data_list
        result['page'] = page
        result['listTotal'] = list_total
        result['shorthand'] = shorthand
        if list_total % pageSize == 0:
            result['totalPage'] = list_total // pageSize
        else:
            result['totalPage'] = list_total // pageSize + 1
    # result['latest_news'] = get_latest_news_list(6)
    # result['hot_news'] = get_hot_news_list(10)
    # result['baidu_news'] = get_top_news_list(10, '百度')
    # result['sogou_news'] = get_top_news_list(10, '搜狗')
    # result['tsz_news'] = get_top_news_list(10, '360')
    result['host'] = HOST
    result['category_id'] = category_id
    if category_id == 'search_data' or category_id == 'page':
        result['category'] = '云禾分发平台'
        result['hot_app_list'] = get_hot_app_list(hot_app_count)
        result['app_article_list'] = get_app_category_article_list(10, '小额贷款')
    else:
        result['category'] = AppCategory.objects.get(id=category_id).category
        result['hot_app_list'] = get_category_hot_app_list(9, category_id)
        result['app_article_list'] = get_app_category_article_list(10, result['category'])
    result['footer_link_list'] = get_friendly_link_list(2)
    return render(request, 'app_category.html', locals())


def app_views(request):
    url = request.get_full_path()
    key = ''
    if 'page' in url:
        page = int(re.search('\d+', url, flags=0).group())
    else:
        page = 1
    pageSize = 10
    if 'search_data' in url:
        key = url.split('/')[url.split('/').index('search_data') + 1]
        key = urllib.parse.unquote(key)
        records = App.objects.filter(title__contains=key).order_by('-id')[
                  (page - 1) * pageSize:page * pageSize]
        list_total = App.objects.filter(title__contains=key).count()
    else:
        records = App.objects.all()[(page - 1) * pageSize: page * pageSize]
        list_total = AppCategoryMapping.objects.count()
    result = dict()
    result['listTotal'] = 0
    result['listPage'] = 0
    result['search_data'] = key
    if records:
        data_list = []
        for record in records:
            data = dict()
            data['id'] = record.id
            data['title'] = record.title
            data['icon_link'] = record.icon_link
            if record.introduce:
                data['introduce'] = record.introduce
            else:
                data[
                    'introduce'] = '是一个贷款服务平台，致力于为上班族、企业主、自由职业者、小微企业主提供贷款申请、搜索、推荐、咨询服务。' \
                                   '通过来借APP即可进行小额贷款、大额贷款、信用贷款、信用卡等的申请，还可通过“来借之家”在线社区交流借款心得、分享“新口子”。'
            data_list.append(data)
        result['data'] = data_list
        result['page'] = page
        result['listTotal'] = list_total
        if list_total % pageSize == 0:
            result['totalPage'] = list_total // pageSize
        else:
            result['totalPage'] = list_total // pageSize + 1
    result['host'] = HOST
    result['category'] = '小额贷款'
    result['hot_app_list'] = get_hot_app_list(hot_app_count)
    result['app_article_list'] = get_app_category_article_list(10, '小额贷款')
    result['footer_link_list'] = get_friendly_link_list(2)
    return render(request, 'app.html', locals())


def get_hot_app_list(count):
    """
    获取所有app的热门app
    :param count:
    :return:
    """
    records = list()
    items = App.objects.values('id', 'title', 'icon_link').order_by('-click_num')[:count]
    for item in items:
        data = dict()
        data['id'] = item['id']
        data['title'] = item['title']
        data['icon_link'] = item['icon_link']
        records.append(data)
    return records


def get_category_hot_app_list(count, category):
    """
    获取对应类别的热门app
    :param count:
    :param category:
    :return:
    """
    records = list()
    items = AppCategoryMapping.objects.values('app_id', ).filter(category_id=category)
    for item in items:
        data = dict()
        record = App.objects.values('id', 'title', 'icon_link', 'click_num').get(id=item['app_id'])
        data['id'] = record['id']
        data['title'] = record['title']
        data['icon_link'] = record['icon_link']
        data['click_num'] = record['click_num']
        records.append(data)
    hot_app_list = sorted(records, key=lambda x: x['click_num'], reverse=True)[:count]
    return hot_app_list


def get_app_category_article_list(count, category_name):
    """
    获取对应类型的相关文章
    :param count:
    :param category_name:
    :return:
    """
    app_category_article_list = list()
    category_id = AppCategory.objects.get(category=category_name).id
    app_items = AppCategoryMapping.objects.values('app_id', ).filter(category_id=category_id)[:count]
    for item in app_items:
        data = dict()
        records = AppArticle.objects.values("id", "title", "add_time").filter(app_id=item['app_id'])
        if records:
            record = random.choice(list(records))
            data['id'] = record['id']
            data['title'] = record['title']
            data['add_time'] = record['add_time']
            app_category_article_list.append(data)
    return app_category_article_list


def get_latest_news_list(count):
    """
    获取最新新闻列表
    :return:count
    """
    news_list = []
    news_records = ArticleNew.objects.values("id", "title", "add_time").order_by('-add_time')[:count]
    for news_record in news_records:
        data = dict()
        data['id'] = news_record['id']
        data['title'] = news_record['title']
        data['add_time'] = news_record['add_time']
        news_list.append(data)
    return news_list


def get_hot_news_list(count):
    """
    获取热门新闻列表
    :param count:
    :return:
    """
    news_list = []
    news_records = ArticleNew.objects.values("id", "title", "add_time").order_by('-hot', '-click_num')[:count]
    for news_record in news_records:
        data = dict()
        data['id'] = news_record['id']
        data['title'] = news_record['title']
        data['add_time'] = news_record['add_time']
        news_list.append(data)
    return news_list


def get_related_news_list(count, id):
    """
    获取相关新闻列表
    :param count:
    :param id:
    :return:
    """
    news_list = []
    keyword_id = ArticleNew.objects.get(id=id).keyword_id
    news_records = ArticleNew.objects.values('id', 'title', 'add_time').filter(keyword_id=keyword_id)
    if len(news_records) > count:
        total_count = len(news_records)
        random_number = random.randint(0, total_count - count)
        news_records = news_records[random_number:random_number + count]
    for news_record in news_records:
        data = dict()
        data['id'] = int(news_record['id'])
        data['title'] = news_record['title']
        data['add_time'] = news_record['add_time']
        news_list.append(data)
    return news_list


def get_top_news_list(count, key):
    """
    获取热搜榜列表
    :param count:
    :param key:
    :return:
    """
    news_list = []
    news_records = SpiderNewsKeyword.objects.values("id", "keyword").filter(keyword_type=key).order_by('-hot')[:count]
    for news_record in news_records:
        data = dict()
        data['keyword'] = news_record['keyword']
        data['id'] = news_record['id']
        news_list.append(data)
    return news_list


def get_related_article_list(count, app_id):
    """
    读取相关文章
    :param count:
    :param app_id:
    :return:
    """
    article_list = []
    article_records = AppArticle.objects.values("id", "title").filter(app_id=app_id)
    if article_records:
        if len(article_records) > count:
            article_records = random.sample(list(article_records), count)
        for article_record in article_records:
            data = dict()
            data['title'] = article_record['title']
            data['id'] = article_record['id']
            article_list.append(data)
        return article_list


def get_related_app_list(count, category_id):
    """
    获取同类别的相关app
    :param count:
    :param category_id:
    :return:
    """
    app_list = []
    app_records = AppCategoryMapping.objects.values('app_id', ).filter(category_id=category_id)
    if app_records:
        if len(app_records) > count:
            app_records = random.sample(list(app_records), count)
        for app_record in app_records:
            data = dict()
            item = App.objects.values("id", "title", "icon_link").get(id=app_record['app_id'])
            data['id'] = item['id']
            data['title'] = item['title']
            data['icon_link'] = item['icon_link']
            app_list.append(data)
        # print(app_list)
        return app_list


def app_detail_views(request, id):
    record = App.objects.filter(id=id)[0]
    item = AppCategoryMapping.objects.get(app_id=id)
    category_id = item.category_id
    result = dict()
    if record.screenshot:
        result['screenshot'] = [url for url in record.screenshot.split(';')]
    else:
        result['screenshot'] = None
    if record.introduce:
        result['id'] = record.id
        result['title'] = record.title
        result['icon_link'] = record.icon_link
        result['introduce'] = record.introduce
    else:
        result['id'] = record.id
        result['title'] = record.title
        result['icon_link'] = record.icon_link
        result['introduce'] = result[
                                  'title'] + '是一款为有借款需求的用户精心打造的借款咨询服务应用软件，它给借款人提供了可随时随地发起的借款咨询服务，' \
                                             '使得借款需求更快速被受理，借款流程更流畅、便捷。'
    result['related_articles'] = read_recommend_article_from_cache(id)
    result['app_related_articles'] = get_app_related_article_list(6, id)
    result['related_app_list'] = read_related_app_from_cache(id, category_id)
    result['search_data'] = ''
    result['host'] = HOST
    result['category_id'] = category_id
    result['category'] = AppCategory.objects.get(id=category_id).category
    result['shorthand'] = AppCategory.objects.get(id=category_id).shorthand
    record = App.objects.get(id=id)
    record.click_num += 1
    record.save()
    result['footer_link_list'] = get_friendly_link_list(2)
    return render(request, 'app_detail.html', locals())


def get_app_related_article_list(count, app_id):
    """
    app相关文章
    :param count:
    :param app_id:
    :return:
    """
    app_related_article_list = list()
    items = AppArticle.objects.values("id", "title").filter(app_id=app_id)
    if items:
        if len(items) > count:
            items = random.sample(list(items), count)
        for item in items:
            data = dict()
            data['id'] = item['id']
            data['title'] = item['title']
            app_related_article_list.append(data)
        return app_related_article_list


def news_detail_views(request, id):
    result = dict()
    result['latest_news'] = get_latest_news_list(6)
    result['hot_news'] = get_hot_news_list(10)
    result['related_news'] = read_related_news_from_cache(6, id)
    record = ArticleNew.objects.filter(id=id)[0]
    data = dict()
    data['title'] = record.title
    data['site'] = record.site
    data['add_time'] = record.add_time
    data['content'] = internal_chain_replace(record.content)
    result['article'] = data
    result['search_data'] = ''
    record.click_num += 1
    record.save()
    result['id'] = id
    result['keyword'] = SpiderNewsKeyword.objects.filter(id=record.keyword_id)[0].keyword
    result['host'] = HOST
    result['url'] = 'news/detail'
    up_list = ArticleNew.objects.values('id', ).filter(id__lt=id).order_by('-id')[:1]
    if up_list:
        result['up_id'] = up_list[0]['id']
    else:
        result['up_id'] = id
    down_list = ArticleNew.objects.values('id', ).filter(id__gt=id).order_by('id')[:1]
    if down_list:
        result['down_id'] = down_list[0]['id']
    else:
        result['down_id'] = id
    result['category'] = SpiderNewsKeyword.objects.get(id=record.keyword_id).keyword
    result['category_id'] = record.keyword_id
    result['footer_link_list'] = get_friendly_link_list(2)
    return render(request, 'news_detail.html', locals())


def get_friendly_link_list(tag):
    """
    获取友情链接列表
    :return:
    """
    items = FriendlyLink.objects.values("title", "link").filter(tag=tag)
    friendly_link_list = list()
    for item in items:
        data = dict()
        data['title'] = item['title']
        data['link'] = item['link']
        friendly_link_list.append(data)
    return friendly_link_list


def internal_chain_replace(article_content):
    """
    内链替换
    :param article_content:
    :return:
    """
    items = InternalChain.objects.all()
    img_list1 = re.findall('<img.*?/>', article_content, re.S)
    for item in items:
        article_content = re.sub('{}'.format(item.word).encode('unicode_escape').decode(),
                                 '<a class="chain" href="{}" target="_blank">{}</a>'.format(item.link, item.word),
                                 str(article_content), count=1)
    img_list2 = re.findall('<img.*?/>', article_content, re.S)
    for i, dd in enumerate(img_list1):
        if img_list1[i] != img_list2[i]:
            article_content = article_content.replace(img_list2[i], img_list1[i])
    return article_content


def news_views(request):
    url = request.get_full_path()
    key = ''
    if 'page' in url:
        page = int(url.split('/')[url.split('/').index('page') + 1])
    else:
        page = 1
    pageSize = 10
    list_total = SpiderNewsKeyword.objects.values('id').count()
    result = dict()
    if list_total % pageSize == 0:
        total_page = list_total // pageSize
    else:
        total_page = list_total // pageSize + 1
    if 'search_data' in url:
        key = url.split('/')[url.split('/').index('search_data') + 1]
        key = urllib.parse.unquote(key)
        records = SpiderNewsKeyword.objects.filter(title__contains=key)
    else:
        if page == total_page:
            records = SpiderNewsKeyword.objects.values('id', 'keyword', 'add_time')[(page - 1) * pageSize:]
        else:
            records = SpiderNewsKeyword.objects.values('id', 'keyword', 'add_time')[
                      (page - 1) * pageSize:page * pageSize]
    result['search_data'] = key
    if records:
        data_list = []
        for record in records:
            data = dict()
            data['id'] = record['id']
            data['keyword'] = record['keyword']
            data['add_time'] = record['add_time']
            data_list.append(data)
        result['data'] = data_list
        result['page'] = page
        result['listTotal'] = list_total
    # result['keyword_id'] = id
    # result['related_search_keywords'] = get_related_search_keywords_list(9, id)
    result['latest_news'] = get_latest_news_list(6)
    result['hot_news'] = get_hot_news_list(10)
    result['host'] = HOST
    result['footer_link_list'] = get_friendly_link_list(2)
    result['category'] = '小额贷款'
    return render(request, 'news.html', locals())


def news_list_views(request):
    url = request.get_full_path()
    key = ''
    id = url.split('/')[3]
    if 'page' in url:
        page = int(url.split('/')[url.split('/').index('page') + 1])
    else:
        page = 1
    pageSize = 10
    list_total = ArticleNew.objects.values('id', ).filter(keyword_id=id).count()
    if list_total % pageSize == 0:
        total_page = list_total // pageSize
    else:
        total_page = list_total // pageSize + 1
    if 'search_data' in url:
        key = url.split('/')[url.split('/').index('search_data') + 1]
        key = urllib.parse.unquote(key)
        records = ArticleNew.objects.values("id", "title", "site",
                                            "add_time").filter(keyword_id=id).filter(title__contains=key)
    else:
        if page == total_page:
            records = ArticleNew.objects.values("id", "title", "site", 'add_time').filter(keyword_id=id)[
                      (page - 1) * pageSize:]
        else:
            records = ArticleNew.objects.values("id", "title", "site", 'add_time').filter(keyword_id=id)[
                      (page - 1) * pageSize:page * pageSize]
    result = dict()
    result['search_data'] = key
    if records:
        data_list = []
        for record in records:
            data = dict()
            data['id'] = record['id']
            data['title'] = record['title']
            data['site'] = record['site']
            data['add_time'] = record['add_time']
            data_list.append(data)
        result['data'] = data_list
        result['page'] = page
        result['listTotal'] = list_total
    result['keyword_id'] = id
    result['related_search_keywords'] = get_related_search_keywords_list(9, id)
    result['latest_news'] = get_latest_news_list(6)
    result['hot_news'] = get_hot_news_list(10)
    result['host'] = HOST
    item = SpiderNewsKeyword.objects.get(id=id)
    result['category'] = item.keyword
    if item.title:
        result['title'] = item.title
    else:
        result['title'] = result['category'] + '_' + result['category'] + '攻略_' + result['category'] + '资讯_云禾分发平台'
    result['category_id'] = id
    result['footer_link_list'] = get_friendly_link_list(2)
    return render(request, 'news_category.html', locals())


def app_category_article_detail_views(request, article_id):
    record = ArticleNew.objects.get(id=article_id)
    result = dict()
    result['id'] = article_id
    result['title'] = record.title
    result['content'] = internal_chain_replace(record.content)
    result['add_time'] = record.add_time
    result['meta_link'] = list()
    if 'https://images.wx273.com/Python/news/' in result['content']:
        result['meta_link'].append(re.search(r'https://images.wx273.com/Python/news/.*?jpg',
                                             result['content'], re.S).group())
    result['host'] = HOST
    result['category'] = SpiderNewsKeyword.objects.get(id=record.keyword_id).keyword_type
    if result['category'] in ['新口子', '短期贷', '小额贷款', '高通过', '放款快', '低利息']:
        result['category_id'] = AppCategory.objects.get(category=result['category']).id
    else:
        result['category'] = '小额贷款'
        result['category_id'] = 3
    up_list = ArticleNew.objects.values("id", ).filter(id__lt=article_id).order_by('-id')
    if up_list:
        result['up_id'] = up_list[0]['id']
    else:
        result['up_id'] = article_id
    down_list = ArticleNew.objects.values("id", ).filter(id__gt=article_id).order_by('id')
    if down_list:
        result['down_id'] = down_list[0]['id']
    else:
        result['down_id'] = article_id
    result['url'] = 'app/article'
    result['search_data'] = ''
    result['latest_news'] = get_latest_news_list(6)
    result['hot_news'] = get_hot_news_list(10)
    result['footer_link_list'] = get_friendly_link_list(2)
    return render(request, 'app_article.html', locals())


def get_latest_app_article_list(count):
    """
    获取最新app文章列表
    :return:count
    """
    news_list = []
    news_records = AppArticle.objects.values("id", "title", "add_time").order_by('-add_time')[:count]
    for news_record in news_records:
        data = dict()
        data['id'] = news_record['id']
        data['title'] = news_record['title']
        data['add_time'] = news_record['add_time']
        news_list.append(data)
    return news_list


def app_article_detail_views(request, article_id):
    record = AppArticle.objects.get(id=article_id)
    result = dict()
    result['id'] = article_id
    result['title'] = record.title
    result['content'] = internal_chain_replace(record.content)
    result['add_time'] = record.add_time
    result['meta_link'] = list()
    if 'https://images.wx273.com/Python/news/' in result['content']:
        result['meta_link'].append(re.search(r'https://images.wx273.com/Python/news/.*?jpg',
                                             result['content'], re.S).group())
    result['host'] = HOST
    result['category_id'] = AppCategoryMapping.objects.get(app_id=record.app_id).category_id
    result['shorthand'] = AppCategory.objects.get(id=result['category_id']).shorthand
    result['category'] = AppCategory.objects.get(id=result['category_id']).category
    result['url'] = 'app/article'
    up_list = AppArticle.objects.values("id", ).filter(id__lt=article_id).order_by('-id')
    if up_list:
        result['up_id'] = up_list[0]['id']
    else:
        result['up_id'] = article_id
    down_list = AppArticle.objects.values("id", ).filter(id__gt=article_id).order_by('id')
    if down_list:
        result['down_id'] = down_list[0]['id']
    else:
        result['down_id'] = article_id
    result['hot_app_list'] = get_hot_app_list(9)
    result['latest_articles'] = get_latest_app_article_list(6)
    result['search_data'] = ''
    result['hot_articles'] = read_hot_article_from_cache(article_id)
    result['related_articles'] = read_related_article_from_cache(article_id)
    result['footer_link_list'] = get_friendly_link_list(2)
    return render(request, 'app_article.html', locals())


def get_related_search_keywords_list(count, p_id):
    """
    获取新闻分类页相关搜索词列表
    :param count:
    :param p_id:
    :return:
    """
    items = SpiderNewsKeywordRelation.objects.values("keyword_id", ).filter(p_id=p_id)
    if items:
        if len(items) > count:
            items = items[:count]
    related_search_keywords_list = list()
    for item in items:
        record = SpiderNewsKeyword.objects.values("id", "keyword").get(id=item['keyword_id'])
        if record:
            data = dict()
            data['category'] = record['keyword']
            data['category_id'] = record['id']
            related_search_keywords_list.append(data)
    return related_search_keywords_list


def get_index_category_app(category_id):
    """
    获取首页分类app
    :param category_id:
    :return:
    """
    index_app_records = []
    records = AppCategoryMapping.objects.values("app_id", ).filter(category_id=category_id)[:6]
    for record in records:
        data = dict()
        item = App.objects.values("id", "title", "icon_link").get(id=record['app_id'])
        data['id'] = item['id']
        data['title'] = item['title']
        data['icon_link'] = item['icon_link']
        index_app_records.append(data)
    return index_app_records


def index_views(request):
    """
    首页展示
    :param request:
    :return:
    """
    if request.method == 'HEAD':
        return HttpResponse('ok')
    else:
        result = dict()
        result['categoryOne'] = read_from_cache(1)
        result['categoryTwo'] = read_from_cache(2)
        result['categoryThree'] = read_from_cache(3)
        result['categoryFour'] = read_from_cache(4)
        result['categoryFive'] = read_from_cache(5)
        result['categorySix'] = read_from_cache(6)
        result['host'] = HOST
        result['search_data'] = ''
        # result['index_news_article_list'] = get_index_news_article_list(10)
        # result['index_app_article_list'] = get_index_app_article_list(10)
        result['index_hot_article_list'] = get_index_hot_article_list(8)
        result['index_latest_article_list'] = get_index_latest_article_list(8)
        result['index_show1_article_list'] = read_index_show_article_list_cache(8, 1)
        result['article_type1'] = SpiderNewsKeyword.objects.get(id=1).keyword_type
        result['index_show2_article_list'] = read_index_show_article_list_cache(8, 2)
        result['article_type2'] = SpiderNewsKeyword.objects.get(id=2).keyword_type
        result['article_type3'] = SpiderNewsKeyword.objects.get(id=3).keyword_type
        result['article_type4'] = SpiderNewsKeyword.objects.get(id=4).keyword_type
        result['index_show3_article_list'] = read_index_show_article_list_cache(8, 3)
        result['index_show4_article_list'] = read_index_show_article_list_cache(8, 4)
        result['friendly_link_list'] = get_friendly_link_list(1)
        result['footer_link_list'] = get_friendly_link_list(2)
        return render(request, 'index.html', locals())


def get_index_hot_article_list(count):
    """
    热门推荐
    :param count:
    :return:
    """
    index_hot_article_list = list()
    try:
        items = ArticleNew.objects.values("id", "title", "add_time", "keyword_id").all().order_by('-click_num')[:count]
    except:
        items = ArticleNew.objects.values("id", "title", "add_time", "keyword_id").all().order_by('-click_num')
    if items:
        for item in items:
            data = dict()
            data['id'] = item['id']
            data['title'] = item['title']
            data['add_time'] = item['add_time']
            data['category_id'] = item['keyword_id']
            data['category'] = SpiderNewsKeyword.objects.get(id=data['category_id']).keyword
            index_hot_article_list.append(data)
        return index_hot_article_list


def get_index_latest_article_list(count):
    """
    最新文章
    :param count:
    :return:
    """
    index_latest_article_list = list()
    today = datetime.now()
    end_time = datetime(today.year, today.month, today.day, 0, 0, 0)
    try:
        items = ArticleNew.objects.values("id", "title",
                                          "add_time",
                                          "keyword_id").filter(add_time__lte=end_time).order_by('-add_time')[:count]
    except:
        items = ArticleNew.objects.values("id", "title",
                                          "add_time",
                                          "keyword_id").filter(add_time__lte=end_time).order_by('-add_time')
    if items:
        for item in items:
            data = dict()
            data['id'] = item['id']
            data['title'] = item['title']
            data['add_time'] = item['add_time']
            data['category_id'] = item['keyword_id']
            data['category'] = SpiderNewsKeyword.objects.get(id=data['category_id']).keyword
            index_latest_article_list.append(data)
        return index_latest_article_list


def get_index_show_article_list(count, k_id):
    """
    首页展示文章
    :param count:
    :param k_id:
    :return:
    """
    index_show_article_list = list()
    keyword_item = SpiderNewsKeyword.objects.get(id=k_id)
    if keyword_item:
        items = SpiderNewsKeywordRelation.objects.values("keyword_id", ).filter(p_id=k_id)
        if items:
            if len(items) > count:
                items = random.sample(list(items), count)
                for item in items:
                    data = dict()
                    data['category_id'] = item['keyword_id']
                    data['category'] = SpiderNewsKeyword.objects.get(id=data['category_id']).keyword
                    article_items = ArticleNew.objects.values("id", "title",
                                                              "add_time").filter(keyword_id=data['category_id'])
                    if article_items:
                        article_item = article_items[0]
                    data['id'] = article_item['id']
                    data['title'] = article_item['title']
                    data['add_time'] = article_item['add_time']
                    index_show_article_list.append(data)
                return index_show_article_list


def get_index_app_article_list(count):
    """
    获取首页app相关文章
    :param count:
    :return:
    """
    index_app_article_list = list()
    total_count = AppArticle.objects.count()
    rand_ids = random.sample(range(1, total_count), count)
    app_records = AppArticle.objects.values("id", "title", "app_id").filter(id__in=rand_ids)
    for app_record in app_records:
        data = dict()
        data['id'] = app_record['id']
        data['title'] = app_record['title']
        data['category_id'] = AppCategoryMapping.objects.get(app_id=app_record['app_id']).category_id
        data['category'] = AppCategory.objects.get(id=data['category_id']).category
        index_app_article_list.append(data)
    return index_app_article_list


def get_index_news_article_list(count):
    """
    获取首页新闻资讯关键词以及相关文章
    :param count:
    :return:
    """
    index_news_article_list = list()
    first_grade_keywords_list = SpiderNewsKeyword.objects.values("id", "keyword").filter(grade=1).order_by('-hot')
    if len(first_grade_keywords_list) > count:
        first_grade_keywords_list = first_grade_keywords_list[:count]
    for first_grade_keyword in first_grade_keywords_list:
        data = dict()
        data['keyword'] = first_grade_keyword['keyword']
        data['keyword_id'] = first_grade_keyword['id']
        item = ArticleNew.objects.values("id", "title").filter(keyword_id=data['keyword_id']).order_by('-hot')
        if item:
            item = item[0]
            data['id'] = item['id']
            data['title'] = item['title']
            index_news_article_list.append(data)
    return index_news_article_list


def app_article_list_views(request):
    """
    展示全部app文章
    :return:
    """
    url = request.get_full_path()
    key = ''
    if 'page' in url:
        page = int(re.search('\d+', url).group())
    else:
        page = 1
    pageSize = 10
    list_total = AppArticle.objects.values('id', ).count()
    if list_total % pageSize == 0:
        total_page = list_total // pageSize
    else:
        total_page = list_total // pageSize + 1
    if 'search_data' in url:
        key = url.split('/')[url.split('/').index('search_data') + 1]
        key = urllib.parse.unquote(key)
        records = AppArticle.objects.values("id", "title").filter(title__contains=key)
    else:
        if page == total_page:
            records = AppArticle.objects.values("id", "title")[(page - 1) * pageSize:]
        else:
            records = AppArticle.objects.values("id", "title")[
                      (page - 1) * pageSize:page * pageSize + 500]
    result = dict()
    result['search_data'] = key
    if records:
        data_list = []
        for record in records:
            data = dict()
            data['id'] = record['id']
            data['title'] = record['title']
            data_list.append(data)
        random.shuffle(data_list)
        result['data'] = data_list[:10]
        result['page'] = page
        result['listTotal'] = list_total
    # result['keyword_id'] = id
    # result['related_search_keywords'] = get_related_search_keywords_list(9, id)
    result['hot_app_list'] = get_hot_app_list(hot_app_count)
    result['latest_articles'] = get_latest_app_article_list(6)
    result['hot_articles'] = read_hot_article_from_cache()
    result['related_articles'] = read_related_article_from_cache()
    result['host'] = HOST
    # item = SpiderNewsKeyword.objects.get(id=id)
    result['category'] = '产品文章列表'
    # if item.title:
    #     result['title'] = item.title
    # else:
    #     result['title'] = result['category'] + '_' + result['category'] + '攻略_' + result['category'] + '资讯_云禾分发平台'
    result['title'] = '产品文章列表'
    result['footer_link_list'] = get_friendly_link_list(2)
    return render(request, 'app_article_list.html', locals())


def credit_class_views(request):
    result = dict()
    result['hot_article_list'] = get_index_hot_article_list(8)
    result['latest_article_list'] = get_index_latest_article_list(8)
    result['hot_app_list'] = get_hot_app_list(9)
    result['categoryOne'] = read_credit_class_article_list_cache(8, 5611)
    result['categoryThree'] = read_credit_class_article_list_cache(8, 5612)
    result['categoryFour'] = read_credit_class_article_list_cache(8, 5613)
    result['categoryFive'] = read_credit_class_article_list_cache(8, 5614)
    result['categorySix'] = read_credit_class_article_list_cache(8, 5615)
    result['categoryTwo'] = read_credit_class_article_list_cache(8, 5616)
    result['host'] = HOST
    result['category'] = '网贷课堂'
    result['search_data'] = ''
    result['friendly_link_list'] = get_friendly_link_list(1)
    result['footer_link_list'] = get_friendly_link_list(2)
    return render(request, 'credit_class.html', locals())


def page_not_found(request, **kwargs):
    result = dict()
    result['host'] = HOST
    response = render(request, '500.html', locals())
    response.status_code = 404
    return response


def page_error(request):
    result = dict()
    result['host'] = HOST
    response = render(request, '500.html', locals())
    response.status_code = 500
    return response

# def sitexml_views(request):
#     return render_to_response('88pxh.xml', content_type="application/xml")
